# -*- coding: utf-8 -*-
"""
# @Time    : 2021/1/8 上午9:15
# @Author  : tgy
# url      : https://leetcode-cn.com/problems/rotate-array/
"""


class Solution:
    ...

    def rotate01(self, nums: list, k: int) -> None:
        ...
        if not nums or len(nums) <= 1:
            return
        k %= len(nums)
        print(nums[-k:])
        print(nums[:-k])
        return nums[-k:] + nums[:-k]
        # for _ in range(k):
        #     nums.insert(0, nums.pop())

    def rotate(self, nums: list, k: int) -> None:
        ...
        if not nums or len(nums) <= 1:
            return
        nums_len = len(nums)
        k = k % nums_len
        # 整体翻转
        self.rotate_part01(nums, 0, nums_len)
        print(nums)
        # 0-k翻转
        self.rotate_part01(nums, 0, k)
        print(nums)
        # 翻转k-nums_len
        self.rotate_part01(nums, k, nums_len)

    def rotate_part(self, nums, start, end):
        """
        范围 [start, end)
        """
        # 这里要 使用()把要进行位运算的括起来，避免把前面不必要的值加进去了
        f_end = start + ((end - start) >> 1)
        for i in range(start, f_end):
            ...
            last = end - 1 - i + start
            nums[i], nums[last] = nums[last], nums[i]


    def rotate_part01(self, nums, start, end):
        """
        范围 [start, end)
        """
        end -= 1
        while start < end:
            nums[start], nums[end] = nums[end], nums[start]
            start += 1
            end -= 1




if __name__ == '__main__':
    ...
    solution = Solution()
    nums = [1, 2, 3, 4]
    solution.rotate01(nums, 2)
    print(nums)
